home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / INSTALL.C < prev    next >
C/C++ Source or Header  |  1990-06-22  |  13KB  |  568 lines

  1. /* install - copy files and set attributes
  2.    Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Copy files and set their permission modes and, if possible,
  19.    their owner and group.  Used similarly to `cp'; typically
  20.    used in Makefiles to copy programs into their destination
  21.    directories.  It can also be used to create the destination
  22.    directories and any leading directories, and to set the final
  23.    directory's modes.  It refuses to copy files onto themselves.
  24.  
  25.    Usage: install [-cs] [-g group] [-m mode] [-o owner]
  26.           [+strip] [+group group] [+mode mode] [+owner owner] file1 file2
  27.  
  28.           install [-cs] [-g group] [-m mode] [-o owner]
  29.           [+strip] [+group group] [+mode mode] [+owner owner] file... dir
  30.  
  31.           install -d [-g group] [-m mode] [-o owner]
  32.           +directory [+group group] [+mode mode] [+owner owner] dir
  33.  
  34.    Options:
  35.    -g, +group GROUP
  36.     Set the group ownership of the installed file or directory
  37.     to the group i.d. of GROUP (default `staff').  GROUP may
  38.     also be a numeric group i.d.
  39.  
  40.    -m, +mode MODE
  41.     Set the permission mode for the installed file or directory
  42.     to MODE, which is an octal number (default 0755).
  43.  
  44.    -o, +owner OWNER
  45.     If run as root, set the ownership of the installed file to
  46.     the user i.d. of OWNER (default `root').  OWNER may also be
  47.     a numeric user i.d.
  48.  
  49.    -c    No effect.  For compatibility with old Unix versions of install.
  50.  
  51.    -s, +strip
  52.     Strip the symbol tables from installed executable files.
  53.  
  54.    -d, +directory
  55.     Create a directory and its leading directories, if they
  56.     do not already exist.  Set the owner, group and mode
  57.     as given on the command line.  Any leading directories
  58.     that are created are also given those attributes.
  59.     This is different from SunOs 4.0 install, which gives
  60.     directories that it creates the default attributes.
  61.  
  62.    David MacKenzie <djm@ai.mit.edu> */
  63.  
  64. #include <stdio.h>
  65. #include <ctype.h>
  66. #include <sys/types.h>
  67. #include <pwd.h>
  68. #include <grp.h>
  69. #include <errno.h>
  70. #include "system.h"
  71. #include "getopt.h"
  72.  
  73. #ifdef STDC_HEADERS
  74. #include <stdlib.h>
  75. #else
  76. char *malloc ();
  77. #endif
  78.  
  79. /* True if C is an ASCII octal digit. */
  80. #define isodigit(c) ((c) >= '0' && c <= '7')
  81.  
  82. /* Number of bytes of a file to copy at a time. */
  83. #define READ_SIZE (32 * 1024)
  84.  
  85. struct passwd *getpwnam ();
  86.  
  87. char *basename ();
  88. char *xmalloc ();
  89. int atoo ();
  90. int change_attributes ();
  91. int copy_file ();
  92. int install_dir ();
  93. int install_file_in_dir ();
  94. int install_file_in_file ();
  95. int isdir ();
  96. void error ();
  97. void get_ids ();
  98. void strip ();
  99. void usage ();
  100.  
  101. extern int errno;
  102.  
  103. /* The name this program was run with, for error messages. */
  104. char *program_name;
  105.  
  106. /* The user name that will own the files, or NULL to not
  107.    change the owner. */
  108. char *owner_name;
  109.  
  110. /* The numeric user i.d. corresponding to `owner_name'. */
  111. int owner_id;
  112.  
  113. /* The group name that will own the files, or NULL to not
  114.    change the group owner. */
  115. char *group_name;
  116.  
  117. /* The numeric group i.d. corresponding to `group_name'. */
  118. int group_id;
  119.  
  120. /* The permissions to which the files will be set.  The umask has
  121.    no effect. */
  122. int mode;
  123.  
  124. /* If nonzero, strip executable files after copying them. */
  125. int strip_files;
  126.  
  127. /* If nonzero, install a directory instead of a regular file. */
  128. int dir_mode;
  129.  
  130. struct option long_options[] =
  131. {
  132.   {"strip", 0, NULL, 's'},
  133.   {"directory", 0, NULL, 'd'},
  134.   {"group", 1, NULL, 'g'},
  135.   {"mode", 1, NULL, 'm'},
  136.   {"owner", 1, NULL, 'o'},
  137.   {NULL, 0, NULL, 0}
  138. };
  139.  
  140. void
  141. main (argc, argv)
  142.      int argc;
  143.      char **argv;
  144. {
  145.   int optc;
  146.   int longind;
  147.   int errors = 0;
  148.  
  149.   program_name = argv[0];
  150.   owner_name = "root";
  151.   group_name = "staff";
  152.   mode = 0755;
  153.   strip_files = 0;
  154.   dir_mode = 0;
  155.   umask (0);
  156.  
  157.   while ((optc = getopt_long (argc, argv, "csdg:m:o:", long_options, &longind))
  158.      != EOF)
  159.     {
  160.       if (optc == 0)        /* Long option. */
  161.     optc = long_options[longind].val;
  162.       switch (optc)
  163.     {
  164.     case 'c':
  165.       break;
  166.     case 's':
  167.       strip_files = 1;
  168.       break;
  169.     case 'd':
  170.       dir_mode = 1;
  171.       break;
  172.     case 'g':
  173.       group_name = optarg;
  174.       break;
  175.     case 'm':
  176.       mode = atoo (optarg);
  177.       if (mode < 0 || mode > 07777)
  178.         error (1, 0, "invalid file mode `%s'", optarg);
  179.       break;
  180.     case 'o':
  181.       owner_name = optarg;
  182.       break;
  183.     default:
  184.       usage ();
  185.     }
  186.     }
  187.  
  188.   switch (argc - optind)
  189.     {
  190.     case 0:
  191.       usage ();
  192.       break;
  193.     case 1:
  194.       if (!dir_mode || strip_files)
  195.     usage ();
  196.       get_ids ();
  197.       errors = install_dir (argv[optind]);
  198.       break;
  199.     case 2:
  200.       if (dir_mode)
  201.     usage ();
  202.       get_ids ();
  203.       if (!isdir (argv[argc - 1]))
  204.     errors = install_file_in_file (argv[optind], argv[argc - 1]);
  205.       else
  206.     errors = install_file_in_dir (argv[optind], argv[argc - 1]);
  207.       break;
  208.     default:
  209.       if (dir_mode || !isdir (argv[argc - 1]))
  210.     usage ();
  211.       get_ids ();
  212.       for (; optind < argc - 1; ++optind)
  213.     errors |= install_file_in_dir (argv[optind], argv[argc - 1]);
  214.       break;
  215.     }
  216.  
  217.   exit (errors);
  218. }
  219.  
  220. /* Make sure directory `path' and all leading directories exist,
  221.    and give it the appropriate attributes.
  222.    If any leading directories are created, they too are given the
  223.    specified attributes.
  224.    Return 0 if successful, 1 if an error occurs. */
  225.  
  226. int
  227. install_dir (path)
  228.      char *path;
  229. {
  230.   char *slash;
  231.   struct stat stats;
  232.  
  233.   if (stat (path, &stats))
  234.     {
  235.       slash = path;
  236.       while (*slash == '/')
  237.     slash++;
  238.       while (slash = index (slash, '/'))
  239.     {
  240.       *slash = 0;
  241.       if (stat (path, &stats))
  242.         {
  243.           if (mkdir (path, 0777))
  244.         {
  245.           error (0, errno, "cannot make directory `%s'", path);
  246.           return 1;
  247.         }
  248.           change_attributes (path);
  249.         }
  250.       else if ((stats.st_mode & S_IFMT) != S_IFDIR)
  251.         {
  252.           error (0, 0, "`%s' is not a directory", path);
  253.           return 1;
  254.         }
  255.       *slash++ = '/';
  256.     }
  257.  
  258.       if (mkdir (path, mode))
  259.     {
  260.       error (0, errno, "cannot make directory `%s'", path);
  261.       return 1;
  262.     }
  263.     }
  264.   else if ((stats.st_mode & S_IFMT) != S_IFDIR)
  265.     {
  266.       error (0, 0, "`%s' is not a directory", path);
  267.       return 1;
  268.     }
  269.  
  270.   return change_attributes (path);
  271. }
  272.  
  273. /* Copy file `from' onto file `to' and give `to' the appropriate
  274.    attributes.
  275.    Return 0 if successful, 1 if an error occurs. */
  276.  
  277. int
  278. install_file_in_file (from, to)
  279.      char *from;
  280.      char *to;
  281. {
  282.   if (copy_file (from, to))
  283.     return 1;
  284.   if (strip_files)
  285.     strip (to);
  286.   return 0;
  287. }
  288.  
  289. /* Copy file `from' into directory `to_dir', keeping its same name,
  290.    and give the copy the appropriate attributes.
  291.    Return 0 if successful, 1 if not. */
  292.  
  293. int
  294. install_file_in_dir (from, to_dir)
  295.      char *from;
  296.      char *to_dir;
  297. {
  298.   char *from_base;
  299.   char *to;
  300.   int ret;
  301.  
  302.   from_base = basename (from);
  303.   to = xmalloc ((unsigned) (strlen (to_dir) + strlen (from_base) + 2));
  304.   sprintf (to, "%s/%s", to_dir, from_base);
  305.   ret = install_file_in_file (from, to);
  306.   free (to);
  307.   return ret;
  308. }
  309.  
  310. /* A chunk of a file being copied. */
  311. static char buffer[READ_SIZE];
  312.  
  313. /* Copy file `from' onto file `to', creating `to' if necessary.
  314.    Return 0 if the copy is successful, 1 if not. */
  315.  
  316. int
  317. copy_file (from, to)
  318.      char *from;
  319.      char *to;
  320. {
  321.   int fromfd, tofd;
  322.   int bytes;
  323.   struct stat from_stats, to_stats;
  324.  
  325.   if (stat (from, &from_stats))
  326.     {
  327.       error (0, errno, "%s", from);
  328.       return 1;
  329.     }
  330.   if ((from_stats.st_mode & S_IFMT) != S_IFREG)
  331.     {
  332.       error (0, 0, "`%s' is not a regular file", from);
  333.       return 1;
  334.     }
  335.   if (stat (to, &to_stats) == 0)
  336.     {
  337.       if ((to_stats.st_mode & S_IFMT) != S_IFREG)
  338.     {
  339.       error (0, 0, "`%s' is not a regular file", to);
  340.       return 1;
  341.     }
  342.       if (from_stats.st_dev == to_stats.st_dev
  343.       && from_stats.st_ino == to_stats.st_ino)
  344.     {
  345.       error (0, 0, "`%s' and `%s' are the same file", from, to);
  346.       return 1;
  347.     }
  348.       if (unlink (to))
  349.     {
  350.       /* If unlink fails, try to proceed anyway.  If we can't change the
  351.          mode and maybe the owner and group, there is no point in
  352.          continuing; leave the original file contents unchanged. */
  353.       if (change_attributes (to))
  354.         return 1;
  355.     }
  356.     }
  357.  
  358.   fromfd = open (from, O_RDONLY, 0);
  359.   if (fromfd == -1)
  360.     {
  361.       error (0, errno, "%s", from);
  362.       return 1;
  363.     }
  364.  
  365.   /* Make sure to open the file in a mode that allows writing. */
  366.   tofd = open (to, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  367.   if (tofd == -1)
  368.     {
  369.       error (0, errno, "%s", to);
  370.       close (fromfd);
  371.       return 1;
  372.     }
  373.  
  374.   while ((bytes = read (fromfd, buffer, READ_SIZE)) > 0)
  375.     if (write (tofd, buffer, bytes) != bytes)
  376.       {
  377.     error (0, errno, "%s", to);
  378.     goto copy_error;
  379.       }
  380.  
  381.   if (bytes == -1)
  382.     {
  383.       error (0, errno, "%s", from);
  384.       goto copy_error;
  385.     }
  386.  
  387.   close (fromfd);
  388.   close (tofd);
  389.   return change_attributes (to);
  390.  
  391.  copy_error:
  392.   close (fromfd);
  393.   close (tofd);
  394.   return 1;
  395. }
  396.  
  397. /* Set the attributes of file or directory `path'.
  398.    Return 0 if successful, 1 if not. */
  399.  
  400. int
  401. change_attributes (path)
  402.      char *path;
  403. {
  404.   if ((chown (path, owner_id, group_id) && errno != EPERM)
  405.       || chmod (path, mode))
  406.     {
  407.       error (0, errno, "%s", path);
  408.       return 1;
  409.     }
  410.   return 0;
  411. }
  412.  
  413. /* Strip the symbol table from the file `path'.
  414.    We could dig the magic number out of the file first to
  415.    determine whether to strip it, but the header files and
  416.    magic numbers vary so much from system to system that making
  417.    it portable would be very difficult.  Not worth the effort. */
  418.  
  419. void
  420. strip (path)
  421.      char *path;
  422. {
  423.   int pid, status;
  424.  
  425.   pid = fork ();
  426.   switch (pid)
  427.     {
  428.     case -1:
  429.       error (1, errno, "cannot fork");
  430.       break;
  431.     case 0:            /* Child. */
  432.       execlp ("strip", "strip", path, (char *) NULL);
  433.       error (1, errno, "cannot run strip");
  434.       break;
  435.     default:            /* Parent. */
  436.       /* Parent process. */
  437.       while (pid != wait (&status))    /* Wait for kid to finish. */
  438.     /* Do nothing. */ ;
  439.       break;
  440.     }
  441. }
  442.  
  443. /* Initialize the user and group ownership of the files to install. */
  444.  
  445. void
  446. get_ids ()
  447. {
  448.   struct passwd *pw;
  449.   struct group *gr;
  450.  
  451.   if (owner_name)
  452.     {
  453.       pw = getpwnam (owner_name);
  454.       if (pw == NULL)
  455.     {
  456.       if (!isnumber (owner_name))
  457.         error (1, 0, "invalid user `%s'", owner_name);
  458.       owner_id = atoi (owner_name);
  459.     }
  460.       else
  461.     owner_id = pw->pw_uid;
  462.       endpwent ();
  463.     }
  464.   else
  465.     owner_id = -1;
  466.  
  467.   if (group_name)
  468.     {
  469.       gr = getgrnam (group_name);
  470.       if (gr == NULL)
  471.     {
  472.       if (!isnumber (group_name))
  473.         error (1, 0, "invalid group `%s'", group_name);
  474.       group_id = atoi (group_name);
  475.     }
  476.       else
  477.     group_id = gr->gr_gid;
  478.       endgrent ();
  479.     }
  480.   else
  481.     group_id = -1;
  482. }
  483.  
  484. /* Return nonzero if `str' is an ASCII representation of a positive
  485.    decimal integer, zero if not. */
  486.  
  487. int
  488. isnumber (str)
  489.      char *str;
  490. {
  491.   if (*str == 0)
  492.     return 0;
  493.   for (; *str; str++)
  494.     if (!isdigit (*str))
  495.       return 0;
  496.   return 1;
  497. }
  498.  
  499. /* If `path' is an existing directory or symbolic link to a directory,
  500.    return nonzero, else 0. */
  501.  
  502. int
  503. isdir (path)
  504.      char *path;
  505. {
  506.   struct stat stats;
  507.  
  508.   return stat (path, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR;
  509. }
  510.  
  511. /* Return the value of the octal digit string `str'.
  512.    Return -1 if `str' does not represent a valid octal number. */
  513.  
  514. int
  515. atoo (str)
  516.      char *str;
  517. {
  518.   int num;
  519.  
  520.   if (*str == 0)
  521.     return -1;
  522.   for (num = 0; isodigit (*str); ++str)
  523.     num = num * 8 + *str - '0';
  524.   return *str ? -1 : num;
  525. }
  526.  
  527. /* Return `name' with any leading path stripped off. */
  528.  
  529. char *
  530. basename (name)
  531.      char *name;
  532. {
  533.   char *base;
  534.  
  535.   base = rindex (name, '/');
  536.   return base ? base + 1 : name;
  537. }
  538.  
  539. /* Allocate `n' bytes of memory dynamically, with error checking. */
  540.  
  541. char *
  542. xmalloc (n)
  543.      unsigned n;
  544. {
  545.   char *p;
  546.  
  547.   p = malloc (n);
  548.   if (p == 0)
  549.     error (1, 0, "virtual memory exhausted");
  550.   return p;
  551. }
  552.  
  553. void
  554. usage ()
  555. {
  556.    fprintf (stderr, "\
  557. Usage: %s [-cs] [-g group] [-m mode] [-o owner]\n\
  558.        [+strip] [+group group] [+mode mode] [+owner owner] file1 file2\n\
  559. \n\
  560.        %s [-cs] [-g group] [-m mode] [-o owner]\n\
  561.        [+strip] [+group group] [+mode mode] [+owner owner] file... dir\n\
  562. \n\
  563.        %s -d [-g group] [-m mode] [-o owner]\n\
  564.        +directory [+group group] [+mode mode] [+owner owner] dir\n",
  565.         program_name, program_name, program_name);
  566.   exit (1);
  567. }
  568.